package com.pachong.service.impl;

import com.common.core.WyyMusicConstant;
import com.common.core.utils.JsonUtil;
import com.common.core.utils.WyyMusicUtils;
import com.domain.bean.dto.MusicAllCommentVo;
import com.domain.bean.dto.MusicCmtUserVo;
import com.domain.bean.dto.MusicCommentVo;
import com.domain.bean.music.MusicComment;
import com.domain.bean.music.MusicUser;
import com.domain.bean.music.Song;
import com.domain.bean.music.SongMenu;
import com.google.gson.Gson;
import com.pachong.service.*;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.xsoup.Xsoup;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author : wangyu
 * Date: 2018-03-08
 * Time: 14:38
 */
@Service
public class MusicServiceImpl implements MusicService {

    private static Logger LOGGER= LoggerFactory.getLogger(MusicServiceImpl.class);

    @Autowired
    private MusicUserService musicUserService;
    @Autowired
    private MusicCommentService musicCommentService;


    @Override
    public Integer handleAllComment(String id, int offset, String cmtType){
        //httpPost请求爬取评论信息
        String cmts = WyyMusicUtils.crawlAjaxUrl(id, offset, cmtType,"");
        LOGGER.warn("查询到的评论信息:"+cmts);

        //评论对象
        MusicAllCommentVo musicAllCommentVo = JsonUtil.getGson().fromJson(cmts, MusicAllCommentVo.class);
        if(musicAllCommentVo == null){
            return null;
        }

        List<MusicCommentVo> comments = musicAllCommentVo.getComments();

        //判断是否有评论
        if(comments != null && !comments.isEmpty()){
            //评论存在时,循环处理评论信息
            for (MusicCommentVo commentVo:comments) {
                this.hadleMusicComment(commentVo,cmtType);
                this.hadleUser(commentVo.getUser());
            }
        }

        //循环递归所有的评论信息
        if(musicAllCommentVo.getMore() != null && musicAllCommentVo.getMore()){
            this.handleAllComment(id, offset+1, cmtType);
        }

        return musicAllCommentVo.getTotal();
    }

    /**
     * 处理评论信息
     */
    private void hadleMusicComment(MusicCommentVo commentVo,String cmtType){
        MusicCmtUserVo userVo = commentVo.getUser();
        MusicComment oldCmt = musicCommentService.selectByCommentId(commentVo.getCommentId());
        if(oldCmt != null){//当评论存在时,不在添加评论
            return;
        }

        MusicComment musicComment = new MusicComment();
        musicComment.setCommentId(commentVo.getCommentId());

        if(StringUtils.isNotBlank(commentVo.getTime())
                && WyyMusicUtils.regexNumber(commentVo.getTime())){
            Date date = new Date(Long.valueOf(commentVo.getTime()));
            musicComment.setCmtDate(date);
        }

        musicComment.setContent(WyyMusicUtils.filterEmoji(commentVo.getContent()));
        musicComment.setLikedCount(commentVo.getLikedCount());
        musicComment.setType(cmtType);
        musicComment.setUserCode(userVo.getUserId());
        musicComment.setUserName(userVo.getNickname());
        musicComment.setCreateDate(new Date());
        musicComment.setCreateBy("sys");

        try {
            musicCommentService.save(musicComment);
        }catch (Exception e){
            LOGGER.error("保存评论信息异常:",e);
        }
    }

    /**
     *  处理用户信息
     */
    private void hadleUser(MusicCmtUserVo userVo){
        //处理用户信息
        MusicUser musicUser = new MusicUser();
        MusicUser byCode = musicUserService.findByCode(userVo.getUserId());
        if(byCode!= null){//当用户存在时,不在添加当前用户
            LOGGER.error("用户信息重复:"+userVo.getUserId());
            return;
        }

        musicUser.setCode(userVo.getUserId());
        musicUser.setNickname(userVo.getNickname());
        musicUser.setAvatarUrl(userVo.getAvatarUrl());
        try {
            LOGGER.error("保存用户信息:"+JsonUtil.toJson(musicUser));
            musicUserService.save(musicUser);
        }catch (Exception e){
            LOGGER.error("保存用户信息异常:",e);
        }
    }

}
